/**
 * @author none
 */
class TEBGameView extends V.GameView {
    // private bg: eui.Image;//背景
    //游戏开始动画
    private startTitle: eui.Image;//开始
    private gameStartGroup: eui.Group;//
    private startTimeout: number = -1;//动画消失计时器
    private gameStartTimeout: number = -1;
    private gameStartAnimator: how.Animation;
    //牌局
    private gameIdGroup: eui.Group;
    private gameIdLabel: eui.Label;//牌局编号
    //桌上牌局信息
    private gameInfoGroup: eui.Group;
    private baseScoreTxt: eui.Label;
    private baseScore: eui.Label;//底注信息
    private gameNumberTxt: eui.Label;
    private gameNumber: eui.Label;//局数
    private gameIndex: number = 0;//局数
    //统计
    private recordGroup: eui.Group;//统计界面
    private betInfoLabel: eui.Label;//下注筹码统计
    private totalBetGroup: eui.Group;//
    private myBetNumber: number = 0;
    private totalBetNumber: number = 0;
    //筹码
    private chipGroup: eui.Group;//筹码group
    private chipPool: Array<eui.Image> = [];//筹码池
    private chipList: Array<eui.Image> = [];//所有下注筹码
    private chipRect: eui.Rect;//筹码飞向范围
    private chipTotal: number = 0;//飘过筹码的份数
    private chipTimeout: number = -1;
    private chipEffectTimeout: number = -1;
    private removeChipTimeout: number = -1;
    private chipPosList = [40, 80, 80, -127];
    //开始倒计时
    private countDownGroup: eui.Group;
    private countDownTime: eui.BitmapLabel;//倒计时文本
    private countDownGames: eui.BitmapLabel;//局数
    private startTime: number = 0;
    private startInterval: number = -1;
    private startTotalTime: number = 3;
    //菜单
    // private menuBg: eui.Image;//背景
    // private menuFlag: boolean = false;//菜单是否打开
    // private btnMenu: how.Button;//菜单
    private backMenuGroup: eui.Group;
    private menuBtnSetting: how.Button;//设置
    private menuBtnIntroduction: how.Button;//介绍
    private menuBtnRecord: how.Button;//记录
    // private menuBtnQuit: how.Button;//退出
    //统计
    private btnGameRecord: how.Button;//统计
    //抢庄
    private grabGroup: eui.Group;//抢庄
    private btnNoGrab: how.Button;//不抢庄
    private btnGrab: how.Button;//抢庄
    private grabInterval: number = -1;//计时器
    private grabTime: number;//默认4秒
    private grabEndFlag: number = 0;//记录开始抢庄时间
    //倍数抢庄
    private grabGroup1: eui.Group;
    private btnNoGrabByTime: eui.Button;//不抢
    private btnGrabByTime0: eui.Button;
    private btnGrabByTime1: eui.Button;
    private btnGrabByTime2: eui.Button;
    private btnGrabByTime3: eui.Button;
    //倍数下注
    private betEndFlag: number = 0;//记录开始下注时间
    private betGroup: eui.Group;
    private btnbetByTime0: eui.Button;
    private btnbetByTime1: eui.Button;
    private btnbetByTime2: eui.Button;
    private btnbetByTime3: eui.Button;
    private btnbetByTime4: eui.Button;
    private betInterval: number = -1;
    private betTime: number = 3;
    //继续游戏
    private continueGroup: eui.Group;//继续游戏
    private btnContinue: how.Button;//继续按钮
    private continueInterval: number = -1;//计时器
    private continueTime: number = 3;//三秒
    //匹配中
    private pipeiTip: eui.Group;//匹配面板
    public intervalNumber: number = -1;//匹配提示动画循环
    private messageLabel: eui.Label;
    private cancelButton: how.Button;//取消匹配
    private pipeiTipTxt: eui.Label;//匹配提示
    //骰子
    private diceGroup: eui.Group;//骰子
    private diceTimeout: number = -1;//定时器
    private diceSoundTime: number = -1;
    private canDiceTimeout: number = -1;//是否可以开骰子
    private canDiceFlag: boolean = false;//是否可以开骰子
    private getDiceFlag: boolean = false;//是否已经获取了结果
    private diceBg: eui.Image;//摇动的骰子
    private diceBottom: eui.Image;//底部
    private dice1: eui.Image;//
    private dice0: eui.Image;//
    private diceTop: eui.Image;//顶部
    private diceOpenFlag: boolean = false;//已经开过骰子
    private reportOpenFlag: boolean = false;
    //发牌
    private firstPlayer: number = -1;//第一个发牌的人
    private sendCardTimeout: number = -1;
    private sendingInterval: number = -1;//开始发牌
    private sendCount: number = 0;
    //标记
    private gameRecordFlag: boolean = false;//是否打开统计
    private playingFlag: boolean = false;//是否游戏中
    /** 游戏类型 */
    private gameType: number = 1;//
    private bankerFlag: boolean = false;//是否为庄家
    //新增
    private countdown: eui.BitmapLabel;//中间倒计时
    private startGroup: eui.Group;
    private startType: eui.Image;
    private allKillAnimator: how.Animation;
    // private allWinAnimator: how.Animation;
    private allWinImage: how.Animation;
    //发牌
    public send3: eui.Group;
    public card0: eui.Image;
    public card7: eui.Image;
    public send2: eui.Group;
    public card6: eui.Image;
    public card5: eui.Image;
    public send1: eui.Group;
    public card4: eui.Image;
    public card3: eui.Image;
    public send0: eui.Group;
    public card2: eui.Image;
    public card1: eui.Image;

    //数据
    /** 是否最后一句 */
    private resultList: Array<TEBResultData> = [];//结束界面数据信息
    /**  */
    private opencardFlag: boolean = false;
    /** 是否打开了结束界面 */
    private resultFlag: boolean = false;//
    /** 面板状态 */
    public isBackstatus: BackStatusConfig = BackStatusConfig.back;//
    /** 游戏状态 */
    private state: TEBGameStatus;
    /** 是否取消匹配成功 */
    public quxiaopipeiSuccess = false;//
    /** 是否抢庄或者下注 */
    private clickedFlag: boolean = false;//
    /** 0 没结束  1正常结束   2分数不够 */
    private lastGame: number = -1;//
    /** 开牌结果 */
    private resultData: any;
    /** 结果面板 */
    private resultWin:TEBResultWindowView;
    /** 庄家椅子号 */
    private banker: number = -1;
    /** 局数 */
    private round: number = 0;
    /** 重新返回界面时候，如果游戏已经结束，而且显示过结束页面 */
    private noShowEver: boolean = true;//
    /** 计时器 */
    private resultTimeout: number = -1;//
    /** 已经叫庄的椅子号 */
    private bankerList: Array<number> = [];
    /** 卡牌处理器 */
    private cardManager: TEBCardManager = new TEBCardManager();//

    public constructor() {
        super();
        this.Res = ["teb_gameScene"];
        this.MAX_PLAYER = 4;
        this.skinName = "TEBGameSceneSkin";
    }

    // // public get resourceList(): Array<string> {
    // //     return ["teb_gameScene"];
    // // }

    /*public start(): void {*/
    protected childrenCreated(): void {
        super.childrenCreated();
        this.initUI();
        this.initParam();
        this.initEvent();
    }
    public initUI() {
        if (AppData.getInstance().currentRoom == null) {
            return;
        }
        this.startGroup.visible = false;
        this.countdown.visible = false;
        this.gameStartGroup.visible = false;
        this.gameStartAnimator.visible = false;
        this.recordGroup.visible = false;
        this.gameRecordFlag = false;
        this.backMenuGroup.visible = false;
        this.continueGroup.visible = false;
        this.baseScore.text = base.Utils.formatCurrency(AppData.getInstance().currentRoom.cellScore);
        this.baseScoreTxt.text = TEBLanguage.baseScore;
        this.gameNumberTxt.text = TEBLanguage.gameNumber;
        // this.removeAllAnimation();
        this.allKillAnimator.visible = false;
        this.allWinImage.visible = false;
    }
    public initParam() {
        this.playingFlag = false;
        // this.menuFlag = false;
        this.chipTotal = 0;
    }
    public initEvent() {
        // this.bg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.bgTap, this);
        //菜单
        // this.btnMenu.addEventListener(egret.TouchEvent.TOUCH_TAP, this.menuTap, this);
        this.menuBtnIntroduction.addEventListener(egret.TouchEvent.TOUCH_TAP, this.introductionTap, this);
        this.menuBtnSetting.addEventListener(egret.TouchEvent.TOUCH_TAP, this.settingTap, this);
        this.menuBtnRecord.addEventListener(egret.TouchEvent.TOUCH_TAP, this.recordTap, this);
        // this.menuBtnQuit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.quitTap, this);
        // this.menuBg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.bgTap, this);
        //按钮
        this.btnContinue.addEventListener(egret.TouchEvent.TOUCH_TAP, this.continueTap, this);
        this.btnGameRecord.addEventListener(egret.TouchEvent.TOUCH_TAP, this.gameRecordTap, this);
        this.cancelButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCancelButton, this);
        //叫庄
        this.btnGrab.addEventListener(egret.TouchEvent.TOUCH_TAP, this.grabTap, this);
        this.btnNoGrab.addEventListener(egret.TouchEvent.TOUCH_TAP, this.noGrabTap, this);
        this.btnNoGrabByTime.addEventListener(egret.TouchEvent.TOUCH_TAP, this.noGrabTap, this);
        this.btnGrabByTime0.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGrab0Button, this);
        this.btnGrabByTime1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGrab1Button, this);
        this.btnGrabByTime2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGrab2Button, this);
        this.btnGrabByTime3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGrab3Button, this);
        //投注
        this.btnbetByTime0.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBet0Button, this);
        this.btnbetByTime1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBet1Button, this);
        this.btnbetByTime2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBet2Button, this);
        this.btnbetByTime3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBet3Button, this);
        this.btnbetByTime4.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBet4Button, this);
    }

    /** 收到网络消息 */
    public onNetData(s:number, d:any):void{
        switch(s){
            case 105:{ //开始准备
                this.noShowEver = false;
                this.state = TEBGameStatus.GS_WK_READY;
                if (d.dwGameStartTime != 0) {
                    this.showGameId(d.dwGameStartTime)
                }
                this.setGameStart(d);
                this.round = d.cbRetsOfRound;
                for (var i = 0; i < 4; i++) {
                    let pv:TEBPlayerView = <TEBPlayerView>this.playerList[i];
                    pv.restart();
                }
                this.removeAllAnimation();
                break;
            }case 106:{ //开始叫庄
                this.clickedFlag = false;
                this.state = TEBGameStatus.GS_WK_CALL_BANKER;
                this.gameType = d.wGameType;
                this.startBanker(d);
                //关闭结果页面
                if (this.resultFlag) {
                    if(this.resultWin) this.resultWin.close();
                    this.resultFlag = false;
                }
                if(D.App.userData.userStatus == UserStatus.PLAY){
                    let pv:TEBPlayerView = <TEBPlayerView>this.playerList[0];
                    pv.showThinking();
                }
                break;
            }case 107:{ //用户抢庄信息
                let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(d.wChairID)];
                pv.setGrabBanker(d.wGameType, d.wCallTimes);
                if (d.wCallTimes != 0) this.bankerList.push(d.wChairID);
                if (d.wChairID == D.App.userData.chairID) {
                    this.closeGrabGroup();
                }
                pv.closeThinking();
                break;
            }case 108:{ //开始下注
                this.clickedFlag = false;
                this.state = TEBGameStatus.GS_WK_CALL_BET;
                if (this.banker != D.App.userData.chairID) {//庄家不需要投注
                    if (d.wGameType == 1) {//普通房直接返回
                        this.callScore(1);
                    } else {
                        this.startCallScore(d);
                    }
                } else {
                    this.startCallScore1();
                }
                for (let i = 0; i < 4; i++) {
                    if (i != this.banker && i != D.App.userData.chairID) {
                        let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(i)];
                        pv.showThinking();
                    }
                }
                break;
            }case 109:{ //玩家加注信息
                this.state = TEBGameStatus.GS_WK_CALL_BET;
                var list = this.getChipList(d.lAddScoreCount);
                if (AppData.getInstance().isGameHide) {
                    this.chipActionByData(list, d.lAddScoreCount);
                } else {
                    this.chipAction(this.c2p(d.wAddScoreUser), list, d.lAddScoreCount);
                }
                //计算倍数
                d.lAddScoreTimes = d.lAddScoreCount / this.cellScore;
                let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(d.wAddScoreUser)];
                pv.setBetScore(d);
                pv.closeThinking();
                if (D.App.userData.chairID == d.wAddScoreUser) {
                    this.closeCallScore();
                    this.updateMyBetScore(d.lAddScoreCount);
                }
                break;
            }case 110:{ //摇骰子结果
                this.state = TEBGameStatus.GS_WK_SHAKE_DICE;
                if (AppData.getInstance().isGameHide) {
                    this.setDiceResultByData(d, this.c2p(d.wFirstGetCardUser));
                } else {
                    this.setDiceResult(d, this.c2p(d.wFirstGetCardUser));
                }
                for (let i = 0; i < this.MAX_PLAYER; i++) {
                    let pv:TEBPlayerView = <TEBPlayerView>this.playerList[i];
                    pv.closeThinking();
                }
                break;
            }case 111:{ //开牌结果
                this.state = TEBGameStatus.GS_WK_OPEN_CARD;
                this.resultData = d;
                //停止上一操作
                this.stopDiceAnimation();
                this.checkOpenCard();
                this.lastGame = d.bGameEndFlg;
                if (d.bGameEndFlg == 1 || d.bGameEndFlg == 2) {
                    // this.gameStart = false;
                }
                for (var i = 0; i < this.MAX_PLAYER; i++) {
                    let pv:TEBPlayerView = <TEBPlayerView>this.playerList[i];
                    pv.closeThinking();
                }
                break;
            }case 112:{ //游戏结束
                break;
            }case 113:{ //用户退出
                break;
            }case 114:{ //庄家宣布
                this.state = TEBGameStatus.GS_WK_SET_BANKER;
                this.closeGrabGroup();
                this.setBankerFlag(D.App.userData.chairID == d.wChairID);
                this.banker = d.wChairID;
                this.runningTime = 0;
                let pv:TEBPlayerView
                if (this.bankerList.length == 1) {//只有一人叫庄   
                    pv = <TEBPlayerView>this.playerList[this.c2p(d.wChairID)];
                    pv.setBankerAnimation();
                } else {
                    if (this.bankerList.length == 0) {//无人叫庄
                        this.bankerList = [0, 1, 2, 3];
                    }
                    if (AppData.getInstance().isGameHide) {
                        this.setBankerByIndex(this.banker);
                    } else {
                        this.startRandomBanker();
                    }
                }
                for (var i = 0; i < this.MAX_PLAYER; i++) {
                    pv = <TEBPlayerView>this.playerList[i];
                    pv.closeThinking();
                }
                break;
            }
        }
    }
    /** 同步桌子 */
    public onSyncData(d:any):void{
        this.showPlayer(65535);

        if (this.state == TEBGameStatus.GS_WK_GAME_OVER) {
            return;
        }
        this.reConnectGame();
        this.gameType = d.wGameType;
        this.round = d.cbRetsOfRound;
        switch (d.cbStatus) {
            case TEBGameStatus.GS_WK_READY:{//准备状态
                this.reConnectReady(d);
                for (let i = 0, score: Array<number> = d.playerScore, len = score.length; i < len; i++) {
                    let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(i)];
                    pv.setPlayerScore(score[i]);//玩家分数
                }
                break;
            }case TEBGameStatus.GS_WK_CALL_BANKER:{//抢庄状态
                this.reConnectCallBanker(d);
                for (let i = 0, score: Array<number> = d.playerScore, len = score.length; i < len; i++) {
                    let player:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(i)];
                    player.setPlayerScore(score[i]);//玩家分数
                    switch (d.cbBankerStatus[i]) {
                        case 0:
                            if (i == D.App.userData.chairID) {
                                //我方没抢庄
                                var cbCountDown = 4 - d.cbCountDown;
                                if (cbCountDown < 0) {
                                    cbCountDown = 0;
                                }
                                this.startBanker(d, cbCountDown);
                            } else {
                                player.showThinking();
                            }
                            break;
                        case 1://抢庄
                            player.setGrabBanker(d.wGameType, d.wCallBankerTimes[i]);
                            this.bankerList.push(i);
                            break;
                        case 2://不抢庄
                            player.setGrabBanker(d.wGameType, d.wCallBankerTimes[i]);
                            break;
                    }
                }
                break;
            }case TEBGameStatus.GS_WK_SET_BANKER:{//庄家宣布状态
                this.reConnectReady(d);
                this.setBanker(d.wBankerUser);
                for (let i = 0; i < d.playerScore.length; i++) {
                    let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(i)];
                    pv.setPlayerScore(d.playerScore[i]);
                    pv.closeThinking();
                }
                break;
            }case TEBGameStatus.GS_WK_CALL_BET:{//投注状态
                this.setBankerFlag(D.App.userData.chairID == d.wBankerUser);
                this.reConnectBet(d);
                let baseScore = AppData.getInstance().currentRoom.cellScore, gemeType = d.wGameType, myFlag = true, lAddScore = d.lAddScore;
                this.setBanker(d.wBankerUser);
                let totalBetScore = 0, myBetScore = 0;
                for (let i = 0, score: Array<number> = d.lplayerScore, len = score.length; i < len; i++) {
                    var addScore = lAddScore[i];
                    let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(i)];
                    if (addScore > 0) {
                        var temp = {
                            wGameType: gemeType,
                            lAddScoreCount: addScore,
                            lplayerScore: score[i],
                            lAddScoreTimes: addScore / baseScore,//计算倍数
                        }
                        pv.setBetScore(temp);
                        this.setChipByData(this.getChipList(addScore));
                    } else {
                        if (i != D.App.userData.chairID && i != this.banker) {
                            pv.showThinking();
                        }
                    }
                    totalBetScore += addScore;
                    if (i == D.App.userData.chairID) {
                        myBetScore = addScore;
                        if (addScore > 0) {
                            myFlag = false;//我已经投注
                        }
                    }
                    pv.setPlayerScore(score[i]);//玩家分数
                }
                if (myFlag && D.App.userData.chairID != d.wBankerUser) {
                    if (d.wGameType == 1) {//普通房直接返回
                        this.callScore(1);
                    } else {
                        this.startCallScore(d, 6 - d.cbCountDown);
                    }
                } else {
                    this.startCallScore1(6 - d.cbCountDown);
                }
                this.setBankerInfoByData(totalBetScore, myBetScore);
                break;
            }case TEBGameStatus.GS_WK_SHAKE_DICE:{//骰子状态
                this.reConnectShakeDice(d);
                this.setBankerFlag(D.App.userData.chairID == d.cbBanker);
                this.setBanker(d.cbBanker);
                let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(d.wFirstGetCardUser)];
                pv.setFirstAnimation();
                let gemeType = d.wGameType, addScore = d.lAddScore;
                var totalBetScore = 0, myBetScore = 0;
                for (var i = 0, score: Array<number> = d.playerScore, len = score.length; i < len; i++) {
                    var addingScore = addScore[i];
                    pv = <TEBPlayerView>this.playerList[this.c2p(i)];
                    if (addingScore > 0) {
                        var temp = {
                            wGameType: gemeType,
                            lAddScoreCount: addingScore,
                            lplayerScore: score[i],
                            lAddScoreTimes: addingScore / this.cellScore,//计算倍数
                        }
                        pv.setBetScore(temp);
                        this.setChipByData(this.getChipList(addingScore));
                        totalBetScore += addingScore;
                        if (i == D.App.userData.chairID) {
                            myBetScore = addingScore;
                        }
                    }
                    pv.setPlayerScore(score[i]);//玩家分数
                    pv.unsendCard();
                    pv.closeThinking();
                }
                this.setBankerInfoByData(totalBetScore, myBetScore);
                break;
            }case TEBGameStatus.GS_WK_OPEN_CARD:{//开奖状态
                this.reConnectOpenCard(d);
                this.cellScore = d.lCellScore;
                this.setBanker(d.cbBanker);
                var cbHandCardData = d.cbHandCardData, cbPrizeCount = d.cbPrizeCount, resultList: Array<TEBResultData> = [],
                    score: Array<number> = d.playerScore, len = score.length;
                //计算庄家倍数
                var lAddScore = d.lAddScore, bankerTime = 0;
                for (var t = 0; t < 4; t++) {
                    if (score[t] > 0) {
                        bankerTime -= lAddScore[t];
                    } else {
                        bankerTime += lAddScore[t];
                    }
                }
                lAddScore[d.cbBanker] = Math.abs(bankerTime);
                var totalBetScore = 0, myBetScore = 0;
                for (var i = 0; i < len; i++) {
                    //玩家界面设置
                    var player:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(i)], cardData = cbHandCardData[i], prize = cbPrizeCount[i];
                    player.setPlayerScore(score[i]);//玩家分数
                    player.setCardResult(cardData, this.cardManager.getCardTypeByData(cardData), prize);
                    //保存开奖结果界面信息
                    var item: TEBResultData = new TEBResultData();
                    item.nickname = player.getData().nickName;
                    item.base = this.cellScore;
                    item.score = prize;
                    item.times = lAddScore[i] / this.cellScore;
                    resultList.push(item);
                    //计算投注信息
                    var addingScore = d.lAddScore[i];
                    totalBetScore += addingScore;
                    if (i == D.App.userData.chairID) {
                        myBetScore = addingScore;
                    }
                    player.closeThinking();
                }
                this.resultList = resultList;
                this.lastGame = d.bGameEndFlg;
                //3秒之后，出现结束界面
                if (this.resultTimeout != -1) {
                    egret.clearTimeout(this.resultTimeout);
                    this.resultTimeout = -1;
                }
                this.resultTimeout = egret.setTimeout(this.chipLeave, this, 3000);
                this.setBankerInfoByData(totalBetScore, myBetScore);
                break;
            }default:
                break;
        }
    }
    /** 设置庄家 */
    private setBanker(c:number):void{
        this.banker = c;
        for (let i = 0; i < this.MAX_PLAYER; i++) {
            let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(i)];
            if(i == c){
                pv.setBanker(true);
            }else{
                pv.setBanker(false);
            }
        }
    }
    /*******************************桌面信息********************************* */
    //牌局编号
    public showGameId(id: string): void {
        var kindIDText = AppData.getInstance().currentGame.id.toString();
        for (var i = 0; i < 4 - AppData.getInstance().currentGame.id.toString().toString().length; i++) {
            kindIDText = "0" + kindIDText;
        }
        if (id) {
            this.gameIdGroup.visible = true;
            var gameId = id.toString();
            var chairID = D.App.userData.chairID + 1;
            var tableID = 1;
            this.gameIdLabel.text = kindIDText + AppData.getInstance().currentRoom.id.toString() + (parseInt(gameId) * chairID)
                + "-" + parseInt(chairID.toString() + tableID.toString()) * 5;
        }
    }
    public setBaseScore(value: number) {
        this.baseScore.text = base.Utils.formatCurrency(value);
    }
    //局数
    public setGameNumber(value: number) {
        this.gameIndex = value;
        this.gameNumber.text = value + "/5";
        this.countDownGames.text = value + "a5";
        this.countDownTime.text = "" + 5;
        this.getDiceFlag = false;
        this.gameInfoGroup.visible = true;
    }
    //牌统计
    public setOpenCardData(list: Array<number>) {
        for (var i = 0, len = list.length; i < len; i++) {
            this["cardNumber" + i].text = list[i] + "";
        }
        this.recordGroup.visible = true;
    }
    public resetRecordCard() {
        for (var i = 0; i < 10; i++) {
            this["cardNumber" + i].text = "0";
        }
        this.recordGroup.visible = false;
    }
    /*******************************骰子动画********************************* */
    private setDiceAnimation() {
        this.reportOpenFlag = false;
        this.diceGroup.visible = true;
        this.diceBottom.visible = false;
        this.dice0.visible = false;
        this.dice1.visible = false;
        this.diceTop.visible = false;
        egret.Tween.removeTweens(this.diceBg);
        this.diceBg.visible = true;
        this.diceBg.rotation = 0;
        if (this.diceTimeout != -1) {
            egret.clearTimeout(this.diceTimeout);
            this.diceTimeout = -1;
        }
        if (AppData.getInstance().isGameHide) {
            return;
        } else {
            egret.Tween.get(this.diceBg).to({ rotation: 20 }, 50).to({ rotation: 0 }, 50).to({ rotation: -20 }, 50).to({ rotation: 0 }, 50);
            if (this.getDiceFlag && this.canDiceFlag) {
                egret.Tween.removeTweens(this.diceBg);
                var tempy = this.diceBg.verticalCenter;
                egret.Tween.get(this.diceBg).to({ verticalCenter: tempy - 10 }, 150).to({ verticalCenter: tempy }, 150).wait(100).call(function () {
                    egret.Tween.removeTweens(this.diceBg);
                    if (this.diceOpenFlag) {
                        this.diceGroup.visible = false;
                    } else {
                        this.playDiceResult();
                    }
                }, this);
            } else {
                this.diceTimeout = egret.setTimeout(this.setDiceAnimation, this, 200);
            }
        }
    }
    private setDiceFlag() {
        this.canDiceFlag = true;
    }
    //正常调用
    private setDiceResult(data: any, index: number) {
        if (this.betInterval != -1) {
            egret.clearTimeout(this.betInterval);
            this.betInterval = -1;
        }
        this.getDiceFlag = true;
        this.dice0.source = "bar_sicbo_" + data.cbDice1 + "_png";
        this.dice1.source = "bar_sicbo_" + data.cbDice2 + "_png";
        this.firstPlayer = index;
    }
    //切放后台时，调用
    private setDiceResultByData(data: any, index: number) {
        egret.Tween.removeTweens(this.diceBg);
        if (this.diceTimeout != -1) {
            egret.clearTimeout(this.diceTimeout);
            this.diceTimeout = -1;
        }
        if (this.betInterval != -1) {
            egret.clearTimeout(this.betInterval);
            this.betInterval = -1;
        }
        this.dice0.source = "bar_sicbo_" + data.cbDice1 + "_png";
        this.dice1.source = "bar_sicbo_" + data.cbDice2 + "_png";
        this.firstPlayer = index;
        this.getDiceFlag = true;
        this.playDiceResult();
    }
    private playDiceResult() {
        this.diceOpenFlag = true;
        if (this.diceTimeout != -1) {
            egret.clearTimeout(this.diceTimeout);
            this.diceTimeout = -1;
        }
        if (this.canDiceTimeout != -1) {
            egret.clearTimeout(this.canDiceTimeout);
            this.canDiceTimeout = -1;
        }
        egret.Tween.removeTweens(this.diceBg);
        this.diceBg.visible = false;
        this.diceBottom.visible = true;
        this.dice0.visible = true;
        this.dice1.visible = true;
        this.diceTop.visible = true;
        this.diceTop.horizontalCenter = 16;
        this.diceTop.verticalCenter = -102;
        this.diceTop.alpha = 1;
        //设置第一个玩家            
        /*this.report(TEBGameSceneModule.request.setFirstAnimation, this.firstPlayer);*/ 
        let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.firstPlayer];
        pv.setFirstAnimation();
        //切放后台时，直接进入下个环节
        // if (AppData.getInstance().isGameHide) {
        //     for (var p = 0; p < 4; p++) {
        //         this.report(TEBGameSceneModule.request.playerCard, p);
        //     }
        //     this.report(TEBGameSceneModule.request.setOpenCardFlag);
        //     this.reportOpenFlag = true;
        // } else {
            egret.Tween.get(this.diceTop).to({ rotation: 75, horizontalCenter: 500, verticalCenter: -400 }, 300).to({ alpha: 0 }, 50);
            //计时器
            if (this.sendCardTimeout != -1) {
                egret.clearTimeout(this.sendCardTimeout);
                this.sendCardTimeout = -1;
            }
            this.sendCardTimeout = egret.setTimeout(this.sendingCard, this, 1200);
        // }
    }
    private stopDiceAnimation() {
        egret.Tween.removeTweens(this.diceBg);
        egret.Tween.removeTweens(this.diceTop);
        this.diceBg.visible = false;
        this.diceTop.visible = false;
        this.dice0.visible = false;
        this.dice1.visible = false;
        this.diceBottom.visible = false;
        if (this.sendCardTimeout != -1) {
            egret.clearTimeout(this.sendCardTimeout);
            this.sendCardTimeout = -1;
        }
        this.diceGroup.visible = false;
        // if (!this.reportOpenFlag) {
            /*this.report(TEBGameSceneModule.request.setOpenCardFlag);*/
            this.resultList = [];
            this.opencardFlag = true;
        //     this.reportOpenFlag = true;
        // }
    }
    /*******************************发牌界面******************************** */
    //开始发牌
    private sendingCard() {
        if (this.sendCardTimeout != -1) {
            egret.clearTimeout(this.sendCardTimeout);
            this.sendCardTimeout = -1;
        }
        this.diceGroup.visible = false;
        if (this.sendingInterval != -1) {
            egret.clearInterval(this.sendingInterval);
            this.sendingInterval = -1;
        }
        var posx = how.Application.app.stage.stageWidth / 2, posy = how.Application.app.stage.stageHeight / 2 - 90;
        var posxList = [-168, -84, 0, 84];
        for (var i = 0; i < 4; i++) {
            this["send" + i].visible = true;
            this["send" + i].y = posy;
            this["send" + i].x = posx + posxList[i];
        }
        for (var i = 0; i < 8; i++) {
            this["card" + i].x = 0;
            if (i % 2 == 0) {
                this["card" + i].y = 0;
            } else {
                this["card" + i].y = 20;
            }
        }
        this.diceTop.visible = false;
        this.sendCount = 0;
        this.sendingInterval = egret.setInterval(this.sendCardGroup, this, 170);
        how.SoundManager.playEffect("bar_send_card_mp3");
    }
    private sendCardGroup() {
        this.sendCardByPlayer(this.firstPlayer, this.sendCount);
        this.firstPlayer++;
        if (this.firstPlayer == 4) {
            this.firstPlayer = 0;
        }
        this.sendCount++;
        if (this.sendCount >= 4) {
            if (this.sendingInterval != -1) {
                egret.clearInterval(this.sendingInterval);
                this.sendingInterval = -1;
            }
        }
    }
    private sendCardByPlayer(index: number, count: number) {
        var indexList = [-154, -153, -152, -15];
        var player = this["player" + index], pos = player.localToGlobal(player.width / 2 + indexList[index], player.height / 2 - 95);
        egret.Tween.get(this["send" + count]).to({ x: pos.x, y: pos.y }, 120).call(function () {
            this.setSendPlayerCard(index, count);
        }, this);
    }
    private setSendPlayerCard(index: number, count: number) {
        var item = this["card" + (count * 2)];
        egret.Tween.get(item).to({ y: 20, x: item.width - 5 }, 150).call(function () {
            this["send" + count].visible = false;
            let pv:TEBPlayerView = this.playerList[index];
            pv.sendCard();
            // // this.report(TEBGameSceneModule.request.playerCard, index);
            if (count >= 3) {
                // // this.report(TEBGameSceneModule.request.setOpenCardFlag);
                this.opencardFlag = true;
                this.reportOpenFlag = true;
            }
        }, this);
    }
    /*******************************匹配界面******************************** */
    //显示匹配界面
    public showPiPeiUI(value: boolean): void {
        this.grabGroup.visible = false;
        this.backMenuGroup.visible = false;
        this.pipeiTipTxt.visible = value;
        // this.menuFlag = false;
        this.gameIdGroup.visible = false;
        this.gameInfoGroup.visible = false;
        this.pipeiTip.visible = true;
        this.playingFlag = false;
        var tipIndex = 0;
        if (this.intervalNumber != -1) {
            egret.clearInterval(this.intervalNumber);
            this.intervalNumber = -1;
        }
        this.intervalNumber = egret.setInterval(() => {
            if (tipIndex == 4) {
                tipIndex = 0;
            }
            var text = LanguageConfig.pipeiTip;
            for (var i = 0; i < tipIndex; i++) {
                text += ".";
            }
            this.messageLabel.text = text;
            tipIndex++;
        }, this, 300);
    }
    //隐藏匹配界面
    public hidePiPeiUI(): void {
        this.pipeiTip.visible = false;
        egret.clearInterval(this.intervalNumber);
        this.intervalNumber = -1;
    }
    /*******************************按钮************************************ */
    // private bgTap() {
    //     // if (this.menuFlag) {
    //         this.backMenuGroup.visible = false;
    //         // this.menuFlag = false;
    //     // }
    // }
    // private menuTap() {
    //     if (this.menuFlag) {
    //         this.backMenuGroup.visible = false;
    //         this.menuFlag = false;
    //     } else {
    //         this.backMenuGroup.visible = true;
    //         this.menuFlag = true;
    //     }
    // }
    private settingTap() {
        // // this.report(TEBGameSceneModule.request.setting);
        how.SettingWindow.show();
    }
    private introductionTap() {
        // // this.report(TEBGameSceneModule.request.introduction);
        how.module.ModuleManager.getInstance().initModule(TEBHelpWindowModule, TEBHelpWindowView, TEBHelpWindowData);
    }
    private recordTap() {
        // // this.report(TEBGameSceneModule.request.record);
        how.module.ModuleManager.getInstance().initModule(TEBGameRecordModule, TEBGameRecordView);
        var windowList = how.WindowManager.getInstance().windowList, len = windowList.length;
        for (var i = 0; i < len; i++) {
            if (windowList[i] instanceof how.Alert) {
                how.WindowManager.getInstance().closeWindow(windowList[i]);
            }
        }
    }
    // public quitTap() {
    //     this.menuGroup.visible = false;
    //     this.menuFlag = false;
    //     if (this.playingFlag) {
    //         how.Alert.show(TEBLanguage.noQuitTip);
    //     } else {
    //         how.Dialog.show(TEBLanguage.noAllowQuit, this.leaveGame, function () { }, this, " ", " ");
    //     }
    // }
    public setPlayingFlag(value: number) {
        if (value == 1 || value == 2) {
            this.playingFlag = false;
        }
    }
    // private leaveGame() {
    //     this.report(TEBGameSceneModule.request.onBackButton);
    // }
    private continueTap() {
        // // this.report(TEBGameSceneModule.request.continue);
        this.resetGame();
        this.resultFlag = false;
    }
    private gameRecordTap() {
        if (this.gameRecordFlag) {
            this.gameRecordFlag = false;
            this.recordGroup.visible = false;
        } else {
            this.gameRecordFlag = true;
            this.recordGroup.visible = true;
        }
    }
    private onCancelButton() {
        // // this.report("onCancelButton");
        this.hallSocket.send(CMDConfig.SEND_STANDUP, {});
        this.isBackstatus = BackStatusConfig.quXiaoPiPei;
        this.quxiaopipeiSuccess = false;
        this.hidePiPeiUI();
        this.continueGroup.visible = true;
    }
    /** 不抢 */
    private noGrab():void {
        if (this.clickedFlag) return;
        this.clickedFlag = true;
        this.hallSocket.send(TEBCMDConfig.TEB_SEND_CALL_BANKER, { bBanker: 0, wBankerTime: 0 });
        this["player0"].setGrabBanker(this.gameType, 0);
    }
    /** 抢庄  */
    private grab(times: number) {
        if (this.clickedFlag) return;
        this.clickedFlag = true;
        this.hallSocket.send(TEBCMDConfig.TEB_SEND_CALL_BANKER, { bBanker: 1, wBankerTime: times });
        this["player0"].setGrabBanker(this.gameType, times);
    }
    //抢庄
    private noGrabTap() {
        this.closeGrabGroup();
        // // this.report(TEBGameSceneModule.request.noGrab);
        this.noGrab();
    }
    private grabTap() {
        this.closeGrabGroup();
        // // this.report(TEBGameSceneModule.request.grab, 1);
        this.grab(1);
    }
    private onGrab0Button() {
        this.closeGrabGroup();
        // // this.report(TEBGameSceneModule.request.grab, parseInt(this.btnGrabByTime0["txt"].text));
        this.grab(parseInt(this.btnGrabByTime0["txt"].text));
    }
    private onGrab1Button() {
        this.closeGrabGroup();
        // // this.report(TEBGameSceneModule.request.grab, parseInt(this.btnGrabByTime1["txt"].text));
        this.grab(parseInt(this.btnGrabByTime1["txt"].text));
    }
    private onGrab2Button() {
        this.closeGrabGroup();
        // // this.report(TEBGameSceneModule.request.grab, parseInt(this.btnGrabByTime2["txt"].text));
        this.grab(parseInt(this.btnGrabByTime2["txt"].text));
    }
    private onGrab3Button() {
        this.closeGrabGroup();
        // // this.report(TEBGameSceneModule.request.grab, parseInt(this.btnGrabByTime3["txt"].text));
        this.grab(parseInt(this.btnGrabByTime3["txt"].text));
    }
    //下注信息
    private callScore(time: number) {
        if (this.clickedFlag) return;
        this.clickedFlag = true;
        this.hallSocket.send(TEBCMDConfig.TEB_SEND_CALL_SCORE, { wScoreTime: time });
    }
    //投注
    private onBet0Button() {
        this.closeCallScore();
        // // this.report(TEBGameSceneModule.request.callScore, parseInt(this.btnbetByTime0["txt"].text));
        this.callScore(parseInt(this.btnbetByTime0["txt"].text));
    }
    private onBet1Button() {
        this.closeCallScore();
        // // this.report(TEBGameSceneModule.request.callScore, parseInt(this.btnbetByTime1["txt"].text));
        this.callScore(parseInt(this.btnbetByTime1["txt"].text));
    }
    private onBet2Button() {
        this.closeCallScore();
        // // this.report(TEBGameSceneModule.request.callScore, parseInt(this.btnbetByTime2["txt"].text));
        this.callScore(parseInt(this.btnbetByTime2["txt"].text));
    }
    private onBet3Button() {
        this.closeCallScore();
        // // this.report(TEBGameSceneModule.request.callScore, parseInt(this.btnbetByTime3["txt"].text));
        this.callScore(parseInt(this.btnbetByTime3["txt"].text));
    }
    private onBet4Button() {
        this.closeCallScore();
        // // this.report(TEBGameSceneModule.request.callScore, parseInt(this.btnbetByTime4["txt"].text));
        this.callScore(parseInt(this.btnbetByTime4["txt"].text));
    }
    /*******************************开始游戏*************************** */
    // {"dwGameStartTime":1497346404,"lCellScore":100,"cbRetsOfRound":1,"cbOpenCardData":[0,0,0,0,0,0,0,0,0,0]}
    public setGameStart(data: any, time: number = 5) {
        this.grabEndFlag = 0;
        this.betEndFlag = 0;
        this.diceGroup.visible = false;
        this.totalBetGroup.visible = false;
        //设置牌局
        var value = data.cbRetsOfRound;
        this.setGameNumber(value);
        this.continueGroup.visible = false;
        this.grabGroup.visible = false;
        this.grabGroup1.visible = false;
        this.chipTotal = 0;
        this.playingFlag = true;
        this.setOpenCardData(data.cbOpenCardData);
        //删除所有筹码
        for (var i = 0, len = this.chipList.length; i < len; i++) {
            egret.Tween.removeTweens(this.chipList[i]);
            this.removeChip(this.chipList[i]);
        }
        this.chipList = [];
        if (value == 1) {
            this.countDownGroup.visible = false;
            this.gameRecordFlag = true;
            this.recordGroup.visible = true;
            //第一局直接播放动画
            this.gameStartAnimation();
        } else {
            this.countDownGroup.visible = true;
            if (time <= 0) {
                time = 0;
            }
            // this.countDownTimeTween(time);
            this.startTotalTime = time;
            if (this.startInterval != -1) {
                egret.clearInterval(this.startInterval);
                this.startInterval = -1;
            }
            if (AppData.getInstance().isGameHide) {
                this.gameStartAnimation();
            } else {
                this.startInterval = egret.setInterval(this.startCountDown, this, 920);
            }
        }
    }
    private startCountDown() {
        this.startTotalTime--;
        if (this.startTotalTime <= 0) {
            this.stopGameStartCountDown();
            this.startTotalTime = 0;
            //倒计时结束之后，播放动画
            this.gameStartAnimation();
        }
        this.countDownTime.text = this.startTotalTime + "";
    }
    //关闭倒计时
    private stopGameStartCountDown() {
        if (this.startInterval != -1) {
            egret.clearInterval(this.startInterval);
            this.startInterval = -1;
        }
        this.startTotalTime = 3;
        this.countDownGroup.visible = false;
    }
    //开始游戏动画
    public startAnimationByTime(time: number) {
        if (this.gameStartTimeout != -1) {
            egret.clearTimeout(this.gameStartTimeout);
            this.gameStartTimeout = -1;
        }
        if (AppData.getInstance().isGameHide) {
            this.gameStartAnimation();
        } else {
            this.gameStartTimeout = egret.setTimeout(this.gameStartAnimation, this, time);
        }
    }
    public gameStartAnimation() {
        if (!AppData.getInstance().isGameHide) {
            // this.gameStartAnimator.visible = true;
            how.SoundManager.playEffect("bar_game_start_0_mp3");
            egret.setTimeout(function () {
                how.SoundManager.playEffect("bar_game_start_mp3");
            }, this, 100);
            this.gameStartAnimator.play();
        } else {
            this.gameStartGroup.visible = true;
        }
    }
    private removeGameStartAnimation() {
        this.gameStartAnimator.stop();
        this.gameStartGroup.visible = false;
        this.gameStartAnimator.visible = false;
    }
    /*******************************开始叫庄*************************** */
    //设置抢庄按钮
    // {"wGameType":1,"wMaxTime":100}
    public startBanker(data: any, time: number = 6) {
        this.grabEndFlag = new Date().getTime();
        this.totalBetGroup.visible = false;
        this.gameType = data.wGameType;
        this.grabTime = time;
        this.playingFlag = true;
        this.removeGameStartAnimation();
        this.countDownGroup.visible = false;
        this.continueGroup.visible = false;

        if (data.wGameType == 1) {
            this.grabGroup.visible = true;
            this.grabGroup1.visible = false;
            this.btnNoGrab["txt"].text = "(" + this.grabTime + ")"
        } else if (data.wGameType == 2) {//倍数叫庄
            if (time >= 4) {
                this.startGroup.visible = true;
                this.startType.source = "bar_start_title_grab_png";
                // egret.Tween.get(this.startType).to({ scaleX: 1.4, scaleY: 1.4, alpha: 1 }, 100).to({ scaleX: 0.8, scaleY: 0.8, alpha: 0.8 }, 70)
                //     .to({ scaleX: 1, scaleY: 1, alpha: 1 }, 70);
            } else {
                this.countDownTimeTween(this.grabTime);
            }
            // this.btnNoGrabByTime["txt"].text = "(" + this.grabTime + ")"
            this.btnNoGrabByTime.visible = true;
            this.grabGroup1.alpha = 0.5;
            this.grabGroup1.visible = true;
            if (!AppData.getInstance().isGameHide) {
                egret.Tween.get(this.grabGroup1).to({ alpha: 1 }, 200);
            } else {
                this.grabGroup1.alpha = 1;
            }
            this.grabGroup.visible = false;
            //最大倍数
            var maxTimes: number = data.wMaxTime;
            if (maxTimes < 3) {
                //倍数小于3时，不能叫庄
                // // this.report(TEBGameSceneModule.request.noGrab);
                this.noGrab();
                this.closeGrabGroup();
            } else {
                if (maxTimes <= 6) {
                    for (var i = 0, btnNum = maxTimes - 2; i < btnNum; i++) {
                        var btn = this["btnGrabByTime" + i];
                        btn.visible = true;
                        btn.x = (1 + i) * 170;
                        btn.y = 0;
                        btn["txt"].text = (3 + i) + "";
                    }
                } else {
                    var list = [3, 3 + Math.floor((maxTimes - 3) / 3), 3 + Math.floor((maxTimes - 3) * 2 / 3), maxTimes];
                    for (var i = 0; i < 4; i++) {
                        var btn = this["btnGrabByTime" + i];
                        btn.visible = true;
                        btn.x = (1 + i) * 170;
                        btn.y = 0;
                        btn["txt"].text = list[i] + "";
                    }
                }
            }
            if (this.grabInterval != -1) {
                egret.clearInterval(this.grabInterval);
                this.grabInterval = -1
            }
            this.grabInterval = egret.setInterval(this.setGrabInterval, this, 1000);
        }
    }
    public setGrabInterval() {
        this.startGroup.visible = false;
        this.startType.source = "";
        this.grabTime--;
        if (this.gameType == 1) {
            // this.btnNoGrab["txt"].text = "(" + this.grabTime + ")";
        } else {
            // this.btnNoGrabByTime["txt"].text = "(" + this.grabTime + ")";
        }
        this.countDownTimeTween(this.grabTime);
        if (this.grabTime <= 0) {
            this.closeGrabGroup();
            // // this.report(TEBGameSceneModule.request.noGrab);
            this.noGrab();
            this.countdown.visible = false;
            this.stopGrabInterval();
        }
    }
    private stopGrabInterval() {
        if (this.grabInterval != -1) {
            egret.clearInterval(this.grabInterval);
            this.grabInterval = -1;
        }
    }
    //关闭抢庄按钮
    public closeGrabGroup() {
        this.grabGroup.visible = false;
        this.grabEndFlag = 0;
        if (AppData.getInstance().isGameHide) {
            this.grabGroup1.visible = false;
            this.btnGrabByTime0.visible = false;
            this.btnGrabByTime1.visible = false;
            this.btnGrabByTime2.visible = false;
            this.btnGrabByTime3.visible = false;
            this.btnGrabByTime0.x = 0;
            this.btnGrabByTime1.x = 0;
            this.btnGrabByTime2.x = 0;
            this.btnGrabByTime3.x = 0;
        } else {
            egret.Tween.get(this.grabGroup1).to({ alpha: 0.5 }, 150).call(function () {
                this.grabGroup1.visible = false;
                this.btnGrabByTime0.visible = false;
                this.btnGrabByTime1.visible = false;
                this.btnGrabByTime2.visible = false;
                this.btnGrabByTime3.visible = false;
                this.btnGrabByTime0.x = 0;
                this.btnGrabByTime1.x = 0;
                this.btnGrabByTime2.x = 0;
                this.btnGrabByTime3.x = 0;
            }, this);
        }
    }
    //设置庄家标志
    private setBankerFlag(value: boolean) {
        this.bankerFlag = value;
        this.stopGrabInterval();
        this.startGroup.visible = false;
        this.countdown.visible = false;
    }
    /*******************************开始投注************************************ */
    public startCallScore(data: any, time: number = 6) {
        this.removeGameStartAnimation();
        this.betEndFlag = new Date().getTime();
        if (data.wGameType == 1) {
            //普通房
            // // this.report(TEBGameSceneModule.response.callScore, 1);
            this.callScore(1);
        } else {
            //倍投房
            this.betGroup.alpha = 0.5;
            this.betGroup.visible = true;
            if (!AppData.getInstance().isGameHide) {
                egret.Tween.get(this.betGroup).to({ alpha: 1 }, 200);
            } else {
                this.betGroup.alpha = 1;
            }
            var list: Array<number> = this.getCallScoreList(data.wMaxTime), len = list.length;
            this.betTime = time > 0 ? time : 0;
            //倒计时按钮
            this.btnbetByTime0.visible = true;
            if (time >= 6) {
                this.startGroup.visible = true;
                this.startType.source = "bar_start_title_bet_png";
                // egret.Tween.get(this.startType).to({ scaleX: 1.4, scaleY: 1.4, alpha: 1 }, 90).to({ scaleX: 0.8, scaleY: 0.8, alpha: 0.8 }, 70)
                //     .to({ scaleX: 1, scaleY: 1, alpha: 1 }, 70);
            } else {
                this.countDownTimeTween(this.betTime);
                // this.btnbetByTime0["timeLabel"].text = "(" + time + ")";
            }
            this.btnbetByTime0.x = 0;
            this.btnbetByTime0.y = 0;
            if (this.betInterval != -1) {
                egret.clearInterval(this.betInterval);
                this.betInterval = -1;
            }
            if (!AppData.getInstance().isGameHide) {
                this.betInterval = egret.setInterval(this.betScore, this, 1000);
            }
            //其他按钮
            for (var i = 1; i <= len; i++) {
                var btn = this["btnbetByTime" + i];
                btn.visible = true;
                btn["txt"].text = "" + list[i - 1];
                btn.x = 170 * i;
                btn.y = 0;
            }
        }
    }
    //庄家显示倒计时
    public startCallScore1(time: number = 6) {
        console.log("startCallScore1");
        this.betTime = time;
        if (time >= 6) {
            this.startGroup.visible = true;
            this.startType.source = "bar_start_title_bet_png";
        } else {
            this.countDownTimeTween(time);
        }
        this.removeGameStartAnimation();
        this.betEndFlag = new Date().getTime();
        if (this.betInterval != -1) {
            egret.clearInterval(this.betInterval);
            this.betInterval = -1;
        }
        this.betInterval = egret.setInterval(this.betScore, this, 1000);
    }
    private resetBetInfo() {
        this.totalBetGroup.visible = true;
        this.myBetNumber = 0;
        this.totalBetNumber = 0;
        if (this.bankerFlag) {
            this.betInfoLabel.text = "0";
        } else {
            this.betInfoLabel.text = "0/0";
        }
    }
    private betScore() {
        this.startGroup.visible = false;
        this.startType.source = "";
        this.betTime--;
        this.countDownTimeTween(this.betTime);
        if (this.betTime <= 0 && !this.bankerFlag) {
            this.closeCallScore();
            egret.clearInterval(this.betInterval);
            this.betInterval = -1;
            this.countdown.visible = false;
            // // this.report(TEBGameSceneModule.request.callScore, 1);
            this.callScore(1);
        }
    }
    private getCallScoreList(max: number): Array<number> {
        var list: Array<number> = [];
        if (max < 6) {
            for (var i = 1; i <= max; i++) {
                list.push(i);
            }
        } else if (max == 6) {
            list = [2, 3, 4, 6];
        } else {
            list = [Math.floor(max / 4), Math.floor(max / 2), Math.floor(max * 3 / 4), max];
            var len = 3;
            //去除相同数字
            for (var j = 0; j < len; j++) {
                if (list[j] == list[j + 1]) {
                    list.splice(j, 1);
                    j--;
                    len--;
                }
            }
        }
        //去除为1的数据
        if (list[0] == 1) {
            list.splice(0, 1);
        }
        return list;
    }
    public closeCallScore() {
        this.betEndFlag = 0;
        if (AppData.getInstance().isGameHide) {
            this.betGroup.visible = false;
            this.btnbetByTime0.visible = false;
            this.btnbetByTime1.visible = false;
            this.btnbetByTime2.visible = false;
            this.btnbetByTime3.visible = false;
            this.btnbetByTime4.visible = false;
            this.btnbetByTime0.x = 0;
            this.btnbetByTime1.x = 0;
            this.btnbetByTime2.x = 0;
            this.btnbetByTime3.x = 0;
            this.btnbetByTime4.x = 0;
        } else {
            egret.Tween.get(this.betGroup).to({ alpha: 0.5 }, 150).call(function () {
                this.betGroup.visible = false;
                this.btnbetByTime0.visible = false;
                this.btnbetByTime1.visible = false;
                this.btnbetByTime2.visible = false;
                this.btnbetByTime3.visible = false;
                this.btnbetByTime4.visible = false;
                this.btnbetByTime0.x = 0;
                this.btnbetByTime1.x = 0;
                this.btnbetByTime2.x = 0;
                this.btnbetByTime3.x = 0;
                this.btnbetByTime4.x = 0;
            }, this);
        }
    }
    /*******************************筹码************************************ */
    private chipAction(index: number, list: Array<number> = [], score: number = 0) {
        this.startGroup.visible = false;
        this.removeGameStartAnimation();
        this.totalBetNumber += score;
        if (this.bankerFlag) {
            this.betInfoLabel.text = "" + base.Utils.formatCurrency(this.totalBetNumber);
        } else {
            this.betInfoLabel.text = base.Utils.formatCurrency(this.myBetNumber) + "/" + base.Utils.formatCurrency(this.totalBetNumber);
        }
        if (list.length == 0) return;
        var indexList = this.chipPosList;
        var player = this["player" + index], chipPos = player.localToGlobal(player.width / 2 + indexList[index], player.height / 2),
            chipList = this.chipList, len = list.length, time = 80, chipRect = this.chipRect, ry = chipRect.top,
            rx = chipRect.left, rw = how.Application.app.stage.stageWidth - chipRect.left - chipRect.right - 70,
            rh = how.Application.app.stage.stageHeight - chipRect.top - chipRect.bottom - 60, chipGroup = this.chipGroup;
        chipGroup.visible = true;
        //音效
        how.SoundManager.playEffect("bar_drop_coin_mp3");
        //加入筹码
        for (var i = 0; i < len; i++) {
            var image: eui.Image = this.getChipFromPool();
            image.source = "bar_chip_" + list[i] + "_png";
            image.scaleX = image.scaleY = 0.2;
            image.alpha = 0;
            image.x = chipPos.x;
            image.y = chipPos.y;
            if (AppData.getInstance().isGameHide) {
                image.x = rx + Math.random() * rw;
                image.y = ry + Math.random() * rh;
            } else {
                this.setAnimationByType(image, time * i, rx + Math.random() * rw, ry + Math.random() * rh, 100);
            }
            chipGroup.addChild(image);
            chipList.push(image);
        }
        this.chipTotal++;
        if (this.chipTotal >= 3) {//开始骰子动画
            this.canDiceFlag = false;
            if (this.betInterval != -1) {
                egret.clearInterval(this.betInterval);
                this.betInterval = -1;
            }
            this.countdown.visible = false;
            this.startGroup.visible = false;
            if (this.diceTimeout != -1) {
                egret.clearTimeout(this.diceTimeout);
                this.diceTimeout = -1;
            }
            if (this.canDiceTimeout != -1) {
                egret.clearTimeout(this.canDiceTimeout);
                this.canDiceTimeout = -1;
            }
            if (this.diceSoundTime != -1) {
                egret.clearTimeout(this.diceSoundTime);
                this.diceSoundTime = -1;
            }
            this.getDiceFlag = false;
            this.canDiceFlag = false;
            this.diceOpenFlag = false;
            this.canDiceTimeout = egret.setTimeout(this.setDiceFlag, this, 3200);
            this.diceTimeout = egret.setTimeout(this.setDiceAnimation, this, 1000);
            this.diceSoundTime = egret.setTimeout(function () {
                how.SoundManager.playEffect("bar_throw_dice_mp3");
            }, this, 1000);
        }
    }
    //切换后台运行
    private chipActionByData(list: Array<number> = [], score: number) {
        this.setChipByData(list);
        this.totalBetNumber += score;
        if (this.bankerFlag) {
            this.betInfoLabel.text = "" + base.Utils.formatCurrency(this.totalBetNumber);
        } else {
            this.betInfoLabel.text = base.Utils.formatCurrency(this.myBetNumber) + "/" + base.Utils.formatCurrency(this.totalBetNumber);
        }
        this.chipTotal++;
        if (this.chipTotal == 3) {//开始骰子动画
            this.canDiceFlag = false;
            if (this.diceTimeout != -1) {
                egret.clearTimeout(this.diceTimeout);
                this.diceTimeout = -1;
            }
            if (this.canDiceTimeout != -1) {
                egret.clearTimeout(this.canDiceTimeout);
                this.canDiceTimeout = -1;
            }
            this.getDiceFlag = false;
            this.canDiceFlag = true;
        }
    }
    private updateMyBetScore(score: number) {
        this.myBetNumber = score;
        this.betInfoLabel.text = base.Utils.formatCurrency(this.myBetNumber) + "/" + base.Utils.formatCurrency(this.totalBetNumber);
    }
    private setAnimationByType(image: eui.Image, time: number, x: number, y: number, tweenTime: number) {
        egret.Tween.get(image).wait(time).to({ alpha: 1 }, 1).to({
            x: x,
            y: y,
            scaleX: 1,
            scaleY: 1
        }, tweenTime);
    }
    private getChipFromPool(): eui.Image {
        var pool = this.chipPool, image: eui.Image;
        if (pool.length == 0) {
            image = new eui.Image();
        } else {
            image = pool.pop();
        }
        return image;
    }
    private removeChip(chip: eui.Image) {
        if (chip.parent) {
            chip.parent.removeChild(chip);
        }
        this.chipPool.push(chip);
    }
    //筹码乱飞
    public setResultChip(index: number, data: Array<any>) {
        var indexList = this.chipPosList;
        var player = this["player" + index], chipPos = player.localToGlobal(player.width / 2 + indexList[index], player.height / 2),
            chipList = this.chipList;
        //飞回庄家
        for (var i = 0, len = chipList.length; i < len; i++) {
            egret.Tween.get(chipList[i]).to({ x: chipPos.x, y: chipPos.y, scaleX: 0, scaleY: 0 }, 300);
        }
        how.SoundManager.playEffect("bar_drop_coin_mp3");
        //飞回玩家
        if (this.chipEffectTimeout != -1) {
            egret.clearTimeout(this.chipEffectTimeout);
            this.chipEffectTimeout = -1;
        }
        this.chipEffectTimeout = egret.setTimeout(this.setPlayerChipEffect, this, 400, index, data);
    }
    //飞回赢家
    private setPlayerChipEffect(index: number, data: Array<any>) {
        var indexList = this.chipPosList;
        var player = this["player" + index], chipPos = player.localToGlobal(player.width / 2 + indexList[index], player.height / 2), chipGroup = this.chipGroup;
        how.SoundManager.playEffect("bar_drop_coin_mp3");
        //删除所有筹码
        for (var i = 0, len = this.chipList.length; i < len; i++) {
            egret.Tween.removeTweens(this.chipList[i]);
            this.removeChip(this.chipList[i]);
        }
        this.chipList = [];
        var time = 10;
        for (var d = 0, len1 = data.length; d < len1; d++) {
            var info = data[d], list = info.value, pi = info.player,
                winPlayer = this["player" + pi], winChipPos = winPlayer.localToGlobal(winPlayer.width / 2 + indexList[pi], winPlayer.height / 2);
            for (var v = 0, lenv = list.length; v < lenv; v++) {
                var image: eui.Image = this.getChipFromPool();
                image.source = "bar_chip_" + list[v] + "_png";
                image.scaleX = image.scaleY = 0.2;
                image.alpha = 1;
                image.x = chipPos.x;
                image.y = chipPos.y;
                chipGroup.addChild(image);
                if (AppData.getInstance().isGameHide) {
                    image.x = winChipPos.x;
                    image.y = winChipPos.y;
                } else {
                    this.setAnimationByType(image, time * v, winChipPos.x, winChipPos.y, 350);
                }
                this.chipList.push(image);
            }
        }
        //删除所有筹码
        if (this.removeChipTimeout != -1) {
            egret.clearTimeout(this.removeChipTimeout);
            this.removeChipTimeout = -1;
        }
        this.removeChipTimeout = egret.setTimeout(this.removeAllChip, this, 1000);
    }
    /** 删除所有筹码 */
    private chipLeave() {
        this.removeAllAnimation();
        //最后一局，移除玩家
        var value: number = this.lastGame;
        if (value == 1 || value == 2) {
            this["player0"].restart();
            //游戏结束界面
            if (!this.resultFlag) {
                if(!this.resultWin) this.resultWin = new TEBResultWindowView();
                this.addChild(this.resultWin);
                this.resultFlag = true;
            }
            var resultList: Array<TEBResultData> = this.resultList;
            //获取不到数据
            if (resultList.length == 0) {
                var data = this.resultData;//获取开牌信息
                if (data == null) return;
                var list = data.cbPrizeCount, cellScore = AppData.getInstance().currentRoom.cellScore;
                //计算庄家倍数
                var wAddScoreTimes = data.wAddScoreTimes, bankerTime = 0;
                for (var t = 0; t < 4; t++) {
                    if (list[t] > 0) {
                        bankerTime -= wAddScoreTimes[t];
                    } else {
                        bankerTime += wAddScoreTimes[t];
                    }
                }
                wAddScoreTimes[data.wBankerUser] = Math.abs(bankerTime);
                for (var i = 0; i < 4; i++) {
                    var item: TEBResultData = new TEBResultData();
                    // // item.nickname = this[this.getPlayerByChair(i)].data.nickName;
                    item.nickname = this.playerList[this.c2p(i)].getData().nickName;
                    item.base = cellScore;
                    item.score = list[i];
                    item.times = wAddScoreTimes[i];
                    resultList.push(item);
                }
            }
            this.noShowEver = true;
            this.resultWin.setInfo(resultList, D.App.userData.chairID, this.banker, this.lastGame, this.round);
        }
    }
    //删除所有筹码
    private removeAllChip() {
        //统计归
        this.totalBetGroup.visible = false;
        this.myBetNumber = 0;
        this.totalBetNumber = 0;
        this.betInfoLabel.text = "0/0";
        //筹码归0
        var chipList = this.chipList;
        for (var i = 0, len = chipList.length; i < len; i++) {
            egret.Tween.removeTweens(chipList[i]);
            this.removeChip(chipList[i]);
        }
        this.chipList = [];
        this.chipGroup.visible = false;
        if (AppData.getInstance().isGameHide) {
            // // this.report(TEBGameSceneModule.request.chipLeave);
            this.chipLeave();
        } else {
            if (this.chipTimeout != -1) {
                egret.clearTimeout(this.chipTimeout);
                this.chipTimeout = -1;
            }
            this.chipTimeout = egret.setTimeout(function () {
                // // this.report(TEBGameSceneModule.request.chipLeave);
                this.chipLeave();
            }, this, 2500);
        }
    }
    /*******************************抢庄 下注倒计时********************** */
    public countDownTimeTween(time: number) {
        this.countdown.visible = true;
        if (time < 0) {
            time = 0;
        }
        this.countdown.text = time + "";
        // if (time > 3) {
        //     egret.Tween.get(this.countdown).to({ scaleX: 1.4, scaleY: 1.4, alpha: 1 }, 120).to({ scaleX: 0.8, scaleY: 0.8, alpha: 0.8 }, 80)
        //         .to({ scaleX: 1, scaleY: 1, alpha: 1 }, 80);
        // }
    }
    /*******************************通杀 通赔**************************** */
    public allKillAnimation() {
        this.allKillAnimator.play();
        how.SoundManager.playEffect("bar_game_kill_effect_mp3");
        egret.setTimeout(function () {
            how.SoundManager.playEffect("bar_game_all_kill_mp3");
        }, this, 1300);
    }
    public allWinAnimation() {
        this.allWinImage.visible = true;
        // this.allWinAnimator.play();
        how.SoundManager.playEffect("bar_game_all_win_mp3");
    }
    public removeAllAnimation() {
        this.allKillAnimator.stop();
        // this.allWinAnimator.stop();
        this.allKillAnimator.visible = false;
        this.allWinImage.visible = false;
    }
    /*******************************继续游戏**************************** */
    //继续游戏按钮去除
    public setContinueGroup(value: boolean) {
        this.continueGroup.visible = value;
        if (value) {
            this.playingFlag = false;
            this.gameIdGroup.visible = false;
            this.gameInfoGroup.visible = false;
        }
    }
    /** 继续游戏 */
    public resetGame() {
        this.initUI();
        this.initParam();
        // if (AppData.getInstance().userData.status == UserStatus.PLAY) {
        //     this.hallSocket.send(CMDConfig.SEND_STANDUP, {});
        //     this.isBackstatus = BackStatusConfig.resumeGame;
        // } else {
        //     this.isBackstatus = BackStatusConfig.pipei;
        //     this.resetRecordCard();
        //     //坐下
        //     this.state = TEBGameStatus.GS_WK_NULL;
        //     // this.chairId = -1;
        //     this.hallSocket.send(CMDConfig.SEND_SIT, {
        //         wTableID: 0,
        //         wChairID: 65535,
        //         szTablePass: AppData.getInstance().currentRoom.szTablePass,
        //         cbPassLen: AppData.getInstance().currentRoom.cbPassLen,
        //         CellScore: AppData.getInstance().currentRoom.cellScore,
        //         PreScore: AppData.getInstance().currentRoom.preScore
        //     });
        //     this.setContinueGroup(false);
        // }
    }
    public setContinueInterval() {
        this.continueTime--;
        this.btnContinue["txt"].text = "(" + this.continueTime + ")";
        if (this.continueTime == 0) {
            this.stopContinueInterval();
        }
    }
    private stopContinueInterval() {
        this.continueTime = 3;
        egret.clearInterval(this.continueInterval);
        this.continueInterval = -1;
    }
    /*****************************断线重连************************************** */
    //重置信息
    public reConnectGame() {
        this.playingFlag = true;
        this.grabGroup.visible = false;
        this.resetGame();
    }
    //相同信息
    public reConnectData(data: any) {
        this.playingFlag = true;
        this.gameInfoGroup.visible = true;
        this.showGameId(data.dwGameStartTime);
        this.setBaseScore(data.lCellScore);
        this.setGameNumber(data.cbRetsOfRound);
        this.setOpenCardData(data.cbOpenCardData);
    }
    //准备阶段重连
    public reConnectReady(data: any) {
        this.reConnectData(data);
        this.continueGroup.visible = false;
        this.grabGroup.visible = false;
        this.grabGroup1.visible = false;
        this.chipTotal = 0;
        if (!data.cbCountDown) {
            data.cbCountDown = 5;
        }
        var time = 5 - data.cbCountDown;
        if (time <= 0) {
            //倒计时结束之后，播放动画
            // this.gameStartAnimation();
            this.countDownGroup.visible = false;
        } else {
            this.countDownGroup.visible = true;
            this.countDownTime.text = time + "";
            this.startTotalTime = time;
            if (this.startInterval != -1) {
                egret.clearInterval(this.startInterval);
                this.startInterval = -1;
            }
            this.startInterval = egret.setInterval(this.startCountDown, this, 900);
        }
    }
    //抢庄状态
    public reConnectCallBanker(data: any) {
        this.reConnectData(data);
    }
    //投注状态
    public reConnectBet(data: any) {
        this.reConnectData(data);
    }
    //骰子状态
    // {"wGameType":2,"dwGameStartTime":11051,"cbStatus":105,"cbBanker":2,"lCellScore":100,"cbRetsOfRound":1,"cbOpenCardData":[1,1,2,0,0,0,1,0,1,2],
    // "playerScore":[41525,32695,23235,40840],"cbDice1":5,"cbDice2":3,"wFirstGetCardUser":1,"lAddScore":[0,0,0,0]} 
    public reConnectShakeDice(data) {
        this.reConnectData(data);
        this.dice0.source = "bar_sicbo_" + data.cbDice1 + "_png";
        this.dice1.source = "bar_sicbo_" + data.cbDice2 + "_png";
        this.firstPlayer = data.wFirstGetCardUser;
        this.diceGroup.visible = true;
        this.diceBg.visible = false;
        this.diceBottom.visible = true;
        this.dice1.visible = true;
        this.dice0.visible = true;
        this.diceTop.visible = true;
        this.diceTop.horizontalCenter = 500;
        this.diceTop.verticalCenter = -250;
        this.diceTop.rotation = 75;
        this.diceTop.alpha = 1;
        if (this.sendCardTimeout != -1) {
            egret.clearTimeout(this.sendCardTimeout);
            this.sendCardTimeout = -1;
        }
        // this.sendCardTimeout = egret.setTimeout(this.sendingCard, this, 1000);
    }
    //开奖
    public reConnectOpenCard(data) {
        this.reConnectData(data);
    }
    //断线重连，筹码
    private setChipByData(list: Array<number>) {
        if (list.length == 0) return;
        var width = how.Application.app.stage.stageWidth, height = how.Application.app.stage.stageHeight;
        var chipList = this.chipList, len = list.length, chipRect = this.chipRect,
            rw = width - chipRect.left - chipRect.right - 70, rh = height - chipRect.top - chipRect.bottom - 60,
            rx = (width - rw) / 2, ry = (height - rh) / 2 - 46, chipGroup = this.chipGroup;
        chipGroup.visible = true;
        for (var i = 0; i < len; i++) {
            var image: eui.Image = this.getChipFromPool();
            image.source = "bar_chip_" + list[i] + "_png";
            image.scaleX = image.scaleY = 1;
            image.alpha = 1;
            image.x = rx + Math.random() * rw;
            image.y = ry + Math.random() * rh;
            chipGroup.addChild(image);
            chipList.push(image);
        }
    }
    private setBankerInfoByData(totalBet: number, myBet: number) {
        this.totalBetGroup.visible = true;
        this.totalBetNumber = totalBet;
        this.myBetNumber = myBet;
        if (this.bankerFlag) {
            this.betInfoLabel.text = "" + base.Utils.formatCurrency(this.totalBetNumber);
        } else {
            this.betInfoLabel.text = base.Utils.formatCurrency(this.myBetNumber) + "/" + base.Utils.formatCurrency(this.totalBetNumber);
        }
    }
    private resizeUI() {
        var width = how.Application.app.stage.stageWidth, height = how.Application.app.stage.stageHeight;
        var chipList = this.chipList, len = chipList.length, chipRect = this.chipRect,
            rw = width - chipRect.left - chipRect.right - 70, rh = height - chipRect.top - chipRect.bottom - 60,
            rx = (width - rw) / 2, ry = (height - rh) / 2 - 46;
        for (var i = 0; i < len; i++) {
            var image = chipList[i];
            image.x = rx + Math.random() * rw;
            image.y = ry + Math.random() * rh;
        }
    }
    /*************************************切换后台***********************************/
    public appHide(state: number) {
        if (this.grabInterval != -1) {
            egret.clearInterval(this.grabInterval);
            this.grabInterval = -1
        }
        if (this.betInterval != -1) {
            egret.clearInterval(this.betInterval);
            this.betInterval = -1;
        }
        switch (state) {
            case TEBGameStatus.GS_WK_CALL_BET:
                if (this.chipTotal >= 3) {
                    this.canDiceFlag = true;
                }
                break;
            case TEBGameStatus.GS_WK_SHAKE_DICE:
                this.getDiceFlag = true;
                this.canDiceFlag = true;
                egret.Tween.removeTweens(this.send0);
                egret.Tween.removeTweens(this.send1);
                egret.Tween.removeTweens(this.send2);
                egret.Tween.removeTweens(this.send3);
                this.send0.visible = false;
                this.send1.visible = false;
                this.send2.visible = false;
                this.send3.visible = false;
                this.stopDiceAnimation();
                this.playDiceResult();
                break;
            case TEBGameStatus.GS_WK_OPEN_CARD:
                this.removeAllChip();
                break;
        }
    }
    public appResume() {
        //计时器
        if (this.grabInterval != -1) {
            egret.clearInterval(this.grabInterval);
            this.grabInterval = -1
        }
        if (this.betInterval != -1) {
            egret.clearInterval(this.betInterval);
            this.betInterval = -1;
        }
        if (this.grabEndFlag > 0) {
            var time = new Date().getTime();
            var temp = Math.floor((time - this.grabEndFlag) / 1000);
            if (temp <= 0) {
                this.closeGrabGroup();
                this.countdown.visible = false;
            } else {
                this.grabTime = temp;
                this.grabInterval = egret.setInterval(this.setGrabInterval, this, 1000);
            }
        } else if (this.betEndFlag > 0) {
            var time = new Date().getTime();
            var temp = Math.floor((time - this.betEndFlag) / 1000);
            if (temp <= 0) {
                this.countdown.visible = false;
                this.closeCallScore();
            } else {
                this.betTime = temp;
                this.betInterval = egret.setInterval(this.betScore, this, 1000);
            }
        }
        this.startGroup.visible = false;
        //游戏开始
        if (this.gameStartTimeout != -1) {
            egret.clearTimeout(this.gameStartTimeout);
            this.gameStartTimeout = -1;
        }
        this.removeGameStartAnimation();
        this.stopGameStartCountDown();
        //发牌
        if (this.sendCardTimeout != -1) {
            egret.clearTimeout(this.sendCardTimeout);
            this.sendCardTimeout = -1;
        }
        if (this.sendingInterval != -1) {
            egret.clearInterval(this.sendingInterval);
            this.sendingInterval = -1;
        }
        egret.Tween.removeTweens(this.send0);
        egret.Tween.removeTweens(this.send1);
        egret.Tween.removeTweens(this.send2);
        egret.Tween.removeTweens(this.send3);
        this.send0.visible = false;
        this.send1.visible = false;
        this.send2.visible = false;
        this.send3.visible = false;
        //骰子
        egret.Tween.removeTweens(this.diceBg);
        if (this.diceTimeout != -1) {
            egret.clearInterval(this.diceTimeout);
            this.diceTimeout = -1;
        }
        if (this.canDiceTimeout != -1) {
            egret.clearTimeout(this.canDiceTimeout);
            this.canDiceTimeout = -1;
        }
        if (this.diceSoundTime != -1) {
            egret.clearTimeout(this.diceSoundTime);
            this.diceSoundTime = -1;
        }
        //筹码
        if (this.chipTimeout != -1) {
            egret.clearTimeout(this.chipTimeout);
            this.chipTimeout = -1;
        }
        if (this.chipEffectTimeout != -1) {
            egret.clearTimeout(this.chipEffectTimeout);
            this.chipEffectTimeout = -1;
        }
        if (this.removeChipTimeout != -1) {
            egret.clearTimeout(this.removeChipTimeout);
            this.removeChipTimeout = -1;
        }
        //动画
        this.removeAllAnimation();//通杀 通赔动画
        this.gameStartAnimator.stop();
        egret.Tween.removeTweens(this.diceBg);
        egret.Tween.removeTweens(this.diceTop);
        egret.Tween.removeTweens(this.countdown);
        this.diceBg.visible = false;
        this.diceTop.visible = false;
        this.dice0.visible = false;
        this.dice1.visible = false;
        this.diceBottom.visible = false;
        this.diceGroup.visible = false;

        var chipList = this.chipList, len = chipList.length;
        var width = how.Application.app.stage.stageWidth, height = how.Application.app.stage.stageHeight;
        var chipList = this.chipList, chipRect = this.chipRect, rw = width - chipRect.left - chipRect.right - 70,
            rh = height - chipRect.top - chipRect.bottom - 60, rx = (width - rw) / 2, ry = (height - rh) / 2 - 46;
        for (var i = 0; i < len; i++) {
            var image: eui.Image = chipList[i];
            egret.Tween.removeTweens(image);
            image.scaleX = image.scaleY = 1;
            image.alpha = 1;
            image.x = rx + Math.random() * rw;
            image.y = ry + Math.random() * rh;
        }
    }
    //销毁
    public onDestroy(): void {
        super.onDestroy();
    }

    //获取下注筹码
    public getChipList(value: number): Array<number> {
        if (value <= 0) return;
        var list: Array<number> = this.getChipsByNumber(value, true);
        while (list.length < 10) {
            var list1 = this.getChipsByNumber(list[0], false);
            if (list1.length == 1) {
                break;
            } else {
                Array.prototype.push.apply(list, list1);
                list.shift();
            }
        }
        return list;
    }
    private getChipsByNumber(value: number, eqFlag: boolean): Array<number> {
        var totalScore = value, list: Array<number> = [], chips = [10, 50, 100, 500, 2000, 10000], i = 5;
        while (totalScore > 0 && i >= 0) {
            var temp = chips[i];
            if (eqFlag) {
                if (totalScore >= temp) {
                    list.push(temp);
                    totalScore -= temp;
                } else {
                    i--;
                }
            } else {
                if (totalScore > temp) {
                    list.push(temp);
                    totalScore -= temp;
                } else {
                    i--;
                    if (totalScore == chips[0]) {
                        list.push(chips[0]);
                        totalScore = 0;
                    }
                }
            }
        }
        return list;
    }
    /** 检查是否可以进行开牌 */
    private checkOpenCard() {
        if (this.opencardFlag) {
            var data = this.resultData;//获取开牌信息
            var player = data.wFirstGetCardUser, cbHandCardData = data.cbHandCardData, time = 1000, list = data.cbPrizeCount;
            //计算庄家倍数
            var wAddScoreTimes = data.wAddScoreTimes, bankerTime = 0;
            for (var t = 0; t < 4; t++) {
                if (list[t] > 0) {
                    bankerTime -= wAddScoreTimes[t];
                } else {
                    bankerTime += wAddScoreTimes[t];
                }
            }
            wAddScoreTimes[data.wBankerUser] = Math.abs(bankerTime);
            for (var p = 0; p < 4; p++) {
                this["player" + p].sendCard();
            }
            var exTime = 0;//增加动画时间
            this.resultList = [];
            for (let i = 0; i < 4; i++) {
                var cardData = cbHandCardData[player];
                var type = this.cardManager.getCardTypeByData(cardData);
                if (type > 20) {
                    exTime += 400;
                }
                let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(i)];
                pv.setOpenCard(time * i + exTime, cardData, type);
                player = player == 3 ? 0 : ++player;

                var item: TEBResultData = new TEBResultData(), score = list[i];
                item.nickname = pv.getData().nickName;
                item.base = this.cellScore;
                item.score = score;
                item.times = wAddScoreTimes[i];
                this.resultList.push(item);
            }
            if (AppData.getInstance().isGameHide) {
                this.setPrizeResult(data.cbPrizeCount, data.cbOpenCardData, data.llPlayerScore);
            } else {
                //检查通杀或者通赔
                var allKill = true, allWin = true;
                if (data.cbPrizeCount[this.banker] > 0) {
                    for (var k = 0; k < 4; k++) {
                        if (k != this.banker && data.cbPrizeCount[k] > 0) {
                            allKill = false;
                        }
                    }
                    allWin = false;
                } else {
                    for (var k = 0; k < 4; k++) {
                        if (k != this.banker && data.cbPrizeCount[k] < 0) {
                            allWin = false;
                        }
                    }
                    allKill = false;
                }
                // allKill = true;
                egret.setTimeout(function () {
                    if (allKill) {
                        this.allKillAnimation();
                    } else if (allWin) {
                        this.allWinAnimation();
                    }
                }, this, 4000 + exTime);

                if (allKill || allWin) {
                    exTime += 1000;
                }
                egret.setTimeout(this.setPrizeResult, this, 4000 + exTime, data.cbPrizeCount, data.cbOpenCardData, data.llPlayerScore);
            }
        }
    }

    /** 设置输赢结果 */
    private setPrizeResult(list: Array<number>, cardData: Array<number>, playerScore: Array<number>) {
        var info = [];
        for (var i = 0; i < this.MAX_PLAYER; i++) {
            let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(i)], score = list[i];
            //设置界面
            pv.setPrizeAnimation(score, playerScore[i]);
            if (score > 0 && i != this.banker) {
                info.push({
                    player: this.c2p(i),
                    value: this.getChipList(score)
                });
            }
        }
        this.setOpenCardData(cardData);
        if (AppData.getInstance().isGameHide) {
            this.removeAllChip();//赢家筹码 
        } else {
            this.setResultChip(this.c2p(this.banker), info);//赢家筹码 
        }
    }
    /** 设置庄家 */
    private startRandomBanker() {
        this.totalTime = 8 + this.banker;
        if (this.bankerInterval != -1) {
            egret.clearInterval(this.bankerInterval);
            this.bankerInterval = -1;
        }
        this.bankerInterval = egret.setInterval(this.runRandomBanker, this, 200);
    }
    private bankerInterval:number;
    private runningTime:number;
    private totalTime:number = 0
    private runRandomBanker() {
        this.runningTime++;
        if (!AppData.getInstance().isGameHide) {
            let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(this.bankerList[(this.runningTime % this.bankerList.length)])];
            pv.startRandomBanker();
        }
        if (this.totalTime <= this.runningTime) {
            egret.clearInterval(this.bankerInterval);
            this.bankerInterval = -1;
            this.setBankerByIndex(this.banker);
        }
    }
    private setBankerByIndex(index: number) {
        if (this.bankerInterval != -1) {
            egret.clearInterval(this.bankerInterval);
            this.bankerInterval = -1;
        }
        for (var i = 0; i < 4; i++) {
            this["player" + i].stopBankerAnimation();
        }
        let pv:TEBPlayerView = <TEBPlayerView>this.playerList[this.c2p(index)];
        pv.setBankerAnimation();
        this.resetBetInfo();
    }
}
